/**
 *<p>Copyright: Copyright (c) 2012</p> 
 *<p>Company: SCPII</p>
 *<p>Project:optimus-manage</p>
 *@author xiezhouyan
 *@Date 2013-5-17 下午4:40:18
 *@version 1.0
 *@description:
 */
package com.cybertron.ironhide.manage.common.json.deserializer;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

import com.cybertron.ironhide.manage.common.base.ImageInfo;

public class ImageInfoDeSerializer extends JsonDeserializer<ImageInfo> {

	public static final String DIRECT_CLASS = "DirectClass";

	public static final String RGB = "RGB";

	@Override
	public ImageInfo deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		Integer width = null;
		Integer height = null;
		Long size = null;
		String colorspace = null;
		while (jp.nextToken() != JsonToken.END_OBJECT) {
			String name = jp.getCurrentName();
			jp.nextToken();
			if (ImageInfo.WIDTH.equals(name)) {
				width = jp.getIntValue();
			} else if (ImageInfo.HEIGHT.equals(name)) {
				height = jp.getIntValue();
			} else if (ImageInfo.SIZE.equals(name)) {
				try {
					size = Long.valueOf(StringUtils.substringBefore(
							jp.getText(), "B"));
				} catch (Exception e) {
					size = 0l;
				}
			} else if (ImageInfo.COLORSPACE.equals(name)) {
				colorspace = StringUtils.trim(StringUtils.remove(jp.getText(),
						DIRECT_CLASS));
			}
		}
		return new ImageInfo(width, height, size, colorspace);
	}
}
